data<-read.table("eQTL_results_R_cis_finished.txt",sep="\t",head=T,colClasses=c("character","integer","numeric","numeric","numeric","integer"))
pos<-read.table("snpsloc.txt",sep="\t",head=T,,colClasses=c("character","character","integer"))
mrg <- merge(data,pos,by="SNP")
mrg<-mrg[with(mrg, order(gene,chrm_snp, pos)), ]
i=1;
temp <- ""
tempchr <- ""
tempdist<-0
main<-c()
k<-0
while(i<nrow(mrg)+1)
{
	print(i)
	if(temp != mrg[i,2])
	{	
		
		if(temp != "")
		{
			if(length(array) > 2)
			{
				combine<-paste(array, collapse=",")
				#stop(combine)
				main<-rbind(main,combine)
			}
		}
		array=c()
		k<-0
		array<-c(array,mrg[i,2],k)
		j=0;
	}
	sep <- mrg[i,8]- tempdist
	if(temp ==mrg[i,2] & tempchr == mrg[i,7] & j != 0 & sep < 50000)
	{
		#print("sucess")
		if(j ==1)
		{
			array<-c(array,mrg[i-1,1])
		}
		#print(mrg[i,1])
		array<-c(array,mrg[i,1])
	}
	else
	{
		if(j != 0 && length(array) > 2)
		{
			combine<-paste(array, collapse=",")
			#print(combine)
			#stop("stop")
			main<-rbind(main,combine)
			k<-k+1
			array<-c()
			array<-c(array,mrg[i,2],k)
		}
		j =0
	}
	tempdist<-mrg[i,8]
	j<-j+1
	tempchr <- mrg[i,7]
	temp <- mrg[i,2]
	i=i+1
}
